package com.getword.controller;

import com.getword.entity.PurchaseMeasureEntity;
import com.getword.util.ReportTemplate;
import com.getword.util.TextField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @classDesc: 功能描述: 车队报表
 * @author: zhuxiang
 * @createTime: 3:28 下午 2020/12/2
 * @version: v1.0
 * @copyright:jh
 */
@Controller
@RequestMapping("/cdReport")
public class JCdReportController
{
    private Logger logger = LoggerFactory.getLogger(JCdReportController.class);

    @RequestMapping("/purchaseShipMeasureExport")
    public void queryshengtiechaxun(HttpServletResponse response, PurchaseMeasureEntity entity)
        throws IOException
    {
        List<PurchaseMeasureEntity> list = new ArrayList<PurchaseMeasureEntity>();
        List<String> columnList = new ArrayList<String>();
        List<TextField> fieldList = new ArrayList();
        columnList.add("车号");
        TextField textField = new TextField("carno");
        textField.setPdf_columnWidth(0.1f);
        fieldList.add(textField);

        columnList.add("合同号");
        textField = new TextField("orderno");
        textField.setPdf_columnWidth(0.1f);
        fieldList.add(textField);

        columnList.add("任务号");
        textField = new TextField("planid");
        textField.setPdf_columnWidth(0.1f);
        fieldList.add(textField);

        columnList.add("品名");
        textField = new TextField("materialname");
        textField.setPdf_columnWidth(0.1f);
        fieldList.add(textField);

        columnList.add("来源");
        textField = new TextField("sourcename");
        textField.setPdf_columnWidth(0.1f);
        fieldList.add(textField);

        columnList.add("去向");
        textField = new TextField("targetname");
        textField.setPdf_columnWidth(0.1f);
        fieldList.add(textField);

        columnList.add("毛重（吨）");
        textField = new TextField("gross");
        textField.setPattern("0.000");
        textField.setPdf_columnWidth(0.06f);
        fieldList.add(textField);

        columnList.add("皮重（吨）");
        textField = new TextField("tare");
        textField.setPattern("0.000");
        textField.setPdf_columnWidth(0.06f);
        fieldList.add(textField);

        columnList.add("净重（吨）");
        textField = new TextField("suttle");
        textField.setPattern("0.0000");
        textField.setPdf_columnWidth(0.06f);
        fieldList.add(textField);

        columnList.add("净重时间");
        textField = new TextField("suttletime");
        textField.setPdf_columnWidth(0.1f);
        fieldList.add(textField);

        ReportTemplate report = new ReportTemplate("");
        report.setTitle("采购船运发运明细");
        report.setColumnHeader(columnList);
        report.setFieldList(fieldList);
        report.setDetail(list);
//        report.setPageHeader("统计时间：" + msrIron.getBegintime() + "至" + msrIron.getEndtime());
//        report.setSummary(sumStr);
        report.setOutputStream(response.getOutputStream());

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String filename = "采购船运发运明细" + dateFormat.format(date);
        filename = new String(filename.getBytes("GBK"), "ISO-8859-1");
        if ("xls".equals(""))
        {
            response.reset();// 清空输出流
            response.setHeader("Content-disposition", "attachment; filename=" + filename + ".xls");// 设定输出文件头
            response.setContentType("application/msexcel");// 定义输出类型
            report.generateExcel();
        }
        else
        {
            float[] widths = new float[fieldList.size()];
            for (int i = 0; i < fieldList.size(); i++)
            {
                TextField tf = (TextField)fieldList.get(i);
                widths[i] = tf.getPdf_columnWidth();
            }
            report.setRelativeWidths(widths);
            response.setContentType("application/xls");
            response.setHeader("Content-disposition", "attachment; filename=" + filename + ".xls");// 设定输出文件头
            response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Pragma", "public");
            response.setDateHeader("Expires", (System.currentTimeMillis() + 1000));
            report.generateExcel();
        }
    }

}
